/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lifecraft;

import java.util.ArrayList;
import java.util.List;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;





final class Time{
        private static Time time= new Time();
        private Time(){    
        }
        
        public static Time get(){
            return time;
        }
        
        private int ch_Tindex;
        private List <Timer> Timers= new ArrayList<Timer>(); // kolekcja timerow
        private long begin = 0;
        static boolean pause = false;
        public int fps = 0;
        public double fpsNumber = 0;
        
        public void initTime(){
            begin = getTime();
            ch_Tindex = add(500);
            putG(ch_Tindex);
        }
        protected long getTime(){
            return (Sys.getTime()*1000)/Sys.getTimerResolution();
        }

        public double getGameTime(String unit){
            if (pause) return (begin/100)/10.0;
            
            if ((unit == "sek")||(unit == "s")){
                return ((getTime()-begin)/100)/10.0;
            }  
            return (getTime()-begin); //ms
        }
        
        public int add(int con){
            Timers.add(new Timer(con));
            return Timers.size()-1;
        }  

        public void put(int index){
            Timers.get(index).memo = (long) getTime();
        
        }
        
        public long howMuchTime(int index){
            return ((long) getTime())-Timers.get(index).memo;
        }
        
        public boolean check(int index){
            
            if(howMuchTime(index)>=Timers.get(index).condition){
                Timers.get(index).memo+=Timers.get(index).condition;
                return true;
            }
            else return false;
        } 
        public boolean check(int index, long con){
            if(howMuchTime(index)>=con){
                Timers.get(index).memo+=con;
                return true;
            }
            else return false;    
        } 
   
        //wszystkie ...G są wzgledem czasu GRY, a nie czasu systemowego
        public void putG(int index){
            Timers.get(index).memo = (long) getGameTime("ms");
        
        }
        
        public long howMuchTimeG(int index){
            return ((long) getGameTime("ms"))-Timers.get(index).memo;
        }
        
        public boolean checkG(int index){
            if (Timers.get(index).memo==0) putG(index);
            
            if(howMuchTimeG(index)>=Timers.get(index).condition){
                Timers.get(index).memo+=Timers.get(index).condition;
                return true;
            }
            else return false;
        } 
        public boolean checkG(int index, long con){
            if(howMuchTimeG(index)>=con){
                Timers.get(index).memo+=con;
                return true;
            }
            else return false;    
        } 
        
        public void updateFpsCounter(){
            if(pause) return;
                
            if(checkG(ch_Tindex)){
                fpsNumber = 2*fps;    
                fps = 0;
            }
            fps++;
        }
        public void pauseGame(){
            if(!pause){
                begin = (long) getGameTime("ms");
                pause = true;
            }
            else{
              begin = getTime() - begin;  
              pause = false;
            }
        }
    }
